import { cloneDeep } from "lodash";
import ComItem from "./comitem";
import DataItem from "./dataItem";

const PropFormType = {
	Base: "base",
	Data: "data",
	Event: "event"
};

const renderForm = (formInfo, type = PropFormType.Base) => {
	if (JSON.stringify(formInfo) === "{}" || formInfo === undefined) return <></>;

	const { config, data } = formInfo;
	const propsData = data.props;

	// 将数据和值合并
	const dataConfig = {};
	const configClone = cloneDeep(config);
	const propsDataClone = cloneDeep(propsData);
	const keys = Object.keys(propsData);
	keys.map(key => {
		const item = config[key];
		if (item) {
			configClone[key].default = propsDataClone[key];
		}
	});

	Object.assign(dataConfig, config);

	switch (type) {
		case PropFormType.Base:
			return (
				<>
					<ComItem config={dataConfig} data={propsDataClone}></ComItem>
				</>
			);
		case PropFormType.Data:
			return (
				<>
					<DataItem config={dataConfig} data={propsDataClone}></DataItem>
				</>
			);
		case PropFormType.Event:
			return (
				<>
					<ComItem config={dataConfig} data={propsDataClone}></ComItem>
				</>
			);
	}
};

export { renderForm, PropFormType };
